require('dotenv').config();
require("./src/model/sync");
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const fileUrl = require("./src/tools/fileUrl");
const path = require("path");

const app = express();
const ChineseMedicineRouter = require("./src/routes/api/ChineseMedicine")

app.use(bodyParser.urlencoded({ limit: "5000mb" }));
//解析JSON格式的请求体数据
app.use(bodyParser.json({ limit: "5000mb", extended: true }));
app.use(cors());

app.use("/uploads", express.static(fileUrl.PDFURL));

//设置允许跨域访问该服务.
app.all("*", function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Content-Type");
  res.header("Access-Control-Allow-Methods", "*");
  next();
});
app.use("/api/chineseMedicine",ChineseMedicineRouter)

//错误处理中间件
app.use(require("./src/routes/errorMiddleware"));

app.use(express.static(__dirname + "/public", { index: "index.html" }));
app.use("*", (req, res) => {
  res.sendFile(path.resolve(__dirname, "public", "index.html"));
});

app.listen(process.env.PORT, () => {
 console.log("dev-auto server listening on ",process.env.PORT );
});
